// import 'babel-polyfill'
import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'
// import './styles/variables.scss'

// import '@/styles/index.scss' // global css
// import '@/assets/samples.css'
import '@/assets/iconfont/iconfont.css'

// import App from './App'
import store from './store'
import router from './router'

import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log

import * as filters from './filters' // global filters
import init from './init'

// 气泡弹窗组件
import Popver from '@/components/Popver'
Vue.component('sPopver', Popver)

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
// import { mockXHR } from '../mock'
// if (process.env.NODE_ENV === 'production') {
//   mockXHR()
// }
// import {
//   getStore
// } from '@/utils/store'

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

const whiteList = ['/login', '/404', '/401']

// tags
// 寻找子菜单的父类
function findMenuParent(tag) {
  const tagCurrent = []
  // const menu = store.getters.menu
  tagCurrent.push(tag)
  return tagCurrent
}

router.afterEach((to, from) => {
  setTimeout(() => {
    const tag = store.getters.tag
    // setTitle(tag.label)
    store.commit('tags/SET_TAG_CURRENT', findMenuParent(tag))
  }, 0)

  if (router.app.isInFramework && to.path.startsWith('/contractManage')) {
    router.app.$frkInstance.$emit('openTag', to) // 给父框架发送新增tag的事件
  }
})

var isTag

window.onpopstate = function(event) {
  isTag = true
}

router.beforeEach((to, from, next) => {
  if (isTag === true) { // 考虑后退和前进按钮的缓存
    isTag = false
    to.query.isTag = true
    next({ path: to.path, query: to.query, replace: true })// 不加replace有bug
    return
  }
  if (to.query.isTag == 'true') {
    to.query.isTag = true
  }
  const value = to.query.src ? to.query.src : to.path
  const label = to.query.name ? to.query.name : to.name
  const name = (to.params || {}).tagsName ? to.params.tagsName : to.meta.title
  if (whiteList.indexOf(value) === -1 && from.name) {
    if (router.app.isInFramework) {
      console.log('当前路由示例', to.meta.title)
      router.app.$frkInstance.$setTagName(to)
    }
    store.commit('tags/ADD_TAG', {
      label: label,
      value: value,
      query: to.query,
      name: name,
      isUpdate: to.name === from.name
    })
  }
  next()
})

function format(num) {
  const reg = /\d{1,3}(?=(\d{3})+$)/g
  return (num + '').replace(reg, '$&,')
}

Vue.prototype.$numFormat = format

Vue.config.productionTip = false

const objInit = init('contract')

export const bootstrap = objInit.bootstrap
export const mount = objInit.mount
export const unmount = objInit.unmount

console.log('contract 1.0.1')
